// +----------------------------------------------------------------------
// | ILDP-three-dimensional系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.lyltop.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 彦麟工作室 <yanlinstudio@126.com>
// +----------------------------------------------------------------------
// | Name: 刘彦麟
// +----------------------------------------------------------------------
// | FILE_NAME: indexedDB.ts
// +----------------------------------------------------------------------
// | Date: 2022/6/16 09:49
// +----------------------------------------------------------------------

import {uuid} from "./uuid";

interface dataFile {
    F_System: string,
    F_Mqtt: string,
    F_ThreeD: string
}

class IndexedDB {
    get isCreate(): boolean {
        return this._isCreate;
    }

    get dataList(): Array<any> {
        return this._dataList;
    }

    private requestDB: any;
    private db: any;
    private _dataList: any[] = [];
    private _isCreate: boolean = false;

    constructor() {
        this.CreateDB();

    }

    private CreateDB() {
        const request = indexedDB.open("ILDP_Three_D", 1);
        request.onerror = event => {
        }

        request.onupgradeneeded = event => {
            // @ts-ignore
            const db = event.target.result;
            this.db = db;
            const store = db.createObjectStore('AB_OA_ThreeDimensional', {
                keyPath: 'F_Id'
            })
            store.createIndex('F_System', 'F_System', {
                unique: false
            })
            store.createIndex('F_ThreeD', 'F_ThreeD', {
                unique: false
            })
            store.createIndex('F_Mqtt', 'F_Mqtt', {
                unique: false
            })
        }
        request.onsuccess = event => {
            // @ts-ignore
            this.db = event.target.result;
            this.GetList();
        }
        this.requestDB = request
    }

    private GetList() {
        const transaction = this.db.transaction(['AB_OA_ThreeDimensional']);
        const objectStore = transaction.objectStore('AB_OA_ThreeDimensional');
        const list: any[] = [];
        // @ts-ignore
        objectStore.openCursor().onsuccess = event => {
            const cursor = event.target.result;
            if (cursor) {
                list.push(cursor.value);
                cursor.continue();
            } else {
                this._dataList = list
            }
            this._isCreate = true
        }
    }

    public Add(objectData: dataFile) {
        const request = this.db.transaction(['AB_OA_ThreeDimensional'], 'readwrite')
            .objectStore('AB_OA_ThreeDimensional')
            .add({
                F_Id: uuid(),
                ...objectData
            })
        // @ts-ignore
        request.onsuccess = event => {
            // 获取数据列表
            this.GetList()
        };
        // @ts-ignore
        request.onerror = event => {
        }
    }

    public Update(objectData: any) {
        const request = this.db.transaction(['AB_OA_ThreeDimensional'], 'readwrite')
            .objectStore('AB_OA_ThreeDimensional')
            .put(objectData)
        // @ts-ignore
        request.onsuccess = event => {
        }
        // @ts-ignore
        request.onerror = event => {
        }
    }

    public Delete(id: string) {
        const request = this.db.transaction(['AB_OA_ThreeDimensional'], 'readwrite')
            .objectStore('AB_OA_ThreeDimensional')
            .delete(id);
        // @ts-ignore
        request.onsuccess = event => {
            this.GetList();
        }
        // @ts-ignore
        request.onerror = event => {
        }

    }
}

export default IndexedDB
